﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HtmlAgilityPack;
using System.Data;
using System.Text.RegularExpressions;

namespace SindSaude
{
    public partial class Cut : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.carregaCut();
        }
        private void carregaCut()
        {
            DataTable mDataTable = new DataTable();
            DataColumn mDataColumn;
            mDataColumn = new DataColumn();
            mDataColumn.DataType = Type.GetType("System.String");
            mDataColumn.ColumnName = "link";
            mDataTable.Columns.Add(mDataColumn);
            mDataColumn = new DataColumn();
            mDataColumn.DataType = Type.GetType("System.String");
            mDataColumn.ColumnName = "img";
            mDataTable.Columns.Add(mDataColumn);
            mDataColumn = new DataColumn();
            mDataColumn.DataType = Type.GetType("System.String");
            mDataColumn.ColumnName = "titulo";
            mDataTable.Columns.Add(mDataColumn);
            HtmlWeb website = new HtmlWeb();
            HtmlAgilityPack.HtmlDocument rootDocument = website.Load("http://www.cut.org.br/");
            var value = rootDocument.DocumentNode.SelectNodes("//div");
            if (value != null)
            {
                foreach (var tag in value)
                {
                    if (tag.Attributes["class"] != null)
                    {
                        if (tag.Attributes["class"].Value == "resp_mob_show")
                        {
                            if (tag.Attributes["id"].Value == "spotlight_responsive")
                            {
                                string strhtml = "";
                                strhtml = tag.InnerHtml.ToString();
                                strhtml = strhtml.Replace("\"", "'").Replace("a href='", "a href='http://www.cut.org.br").Replace("img src='", "img src='http://www.cut.org.br").ToString();

                                List<string> listHref = new List<string>();
                                Regex urlHref = new Regex("href\\s*=\\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\\S+))", RegexOptions.IgnoreCase);
                                MatchCollection matchesHref = urlHref.Matches(strhtml);
                                foreach (Match match in matchesHref)
                                {
                                    listHref.Add(match.Value);
                                }

                                listHref = listHref.Distinct().ToList();

                                List<string> listTitulo = new List<string>();
                                Regex urlTitle = new Regex("title\\s*=\\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\\S+))", RegexOptions.IgnoreCase);
                                MatchCollection matchesTitulo = urlTitle.Matches(strhtml);
                                foreach (Match match in matchesTitulo)
                                {
                                    listTitulo.Add(match.Value);
                                }

                                listTitulo = listTitulo.Distinct().ToList();

                                List<string> listImg = new List<string>();
                                Regex urlImg = new Regex("src\\s*=\\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\\S+))", RegexOptions.IgnoreCase);
                                MatchCollection matchesImg = urlImg.Matches(strhtml);
                                foreach (Match match in matchesImg)
                                {
                                    listImg.Add(match.Value);
                                }

                                DataTable dt = new DataTable();
                                dt.Columns.Add("listHref");
                                dt.Columns.Add("listTitulo");
                                dt.Columns.Add("listImg");
                                for (int i = 0; i < listHref.Count; i++)
                                {
                                    DataRow linha;
                                    linha = dt.NewRow();
                                    linha["listHref"] = listHref[i].ToString().Replace("href=", "").Replace("'", "");
                                    linha["listTitulo"] = listTitulo[i].ToString().Replace("title=", "").Replace("'", "");
                                    linha["listImg"] = listImg[i].ToString().Replace("src=", "").Replace("'", "");
                                    dt.Rows.Add(linha);
                                }
                                rpt_Imagens.DataSource = dt;
                                rpt_Imagens.DataBind();
                            }
                        }
                    }
                }
            }
        }
    }
}